package com.cnzhy.services.impl;

import com.cnzhy.entity.CrmAdminUser;
import com.cnzhy.mapper.CrmAdminUserMapper;
import com.cnzhy.services.interfaces.CrmAdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: zhaoyan5
 */
@Service
public class CrmAdminUserServiceImpl implements CrmAdminUserService {

    private CrmAdminUserMapper crmAdminUserMapper;

    @Autowired
    public CrmAdminUserServiceImpl(CrmAdminUserMapper crmAdminUserMapper) {
        this.crmAdminUserMapper = crmAdminUserMapper;
    }

    @Override
    public int deleteByPrimaryKey(Long userId) {
        return crmAdminUserMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public int insert(CrmAdminUser record) {
        return crmAdminUserMapper.insert(record);
    }

    @Override
    public int insertSelective(CrmAdminUser record) {
        return crmAdminUserMapper.insertSelective(record);
    }

    @Override
    @Transactional
    public CrmAdminUser selectByPrimaryKey(Long userId) {
        return crmAdminUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public int updateByPrimaryKeySelective(CrmAdminUser record) {
        return crmAdminUserMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CrmAdminUser record) {
        return crmAdminUserMapper.updateByPrimaryKey(record);
    }

    @Override
    public void updateUserNameByUserId(Long userId, String userName) {
        crmAdminUserMapper.updateUserNameByUserId(userId, userName);
    }

    @Override
    public List<CrmAdminUser> selectAllCrmAdminUsers() {
        return crmAdminUserMapper.selectAllCrmAdminUsers();
    }

    /**
     * Transactional注解 可以作用在接口、类、类方法
     * 作用于类：当把@Transactional 注解放在类上时，表示所有该类的public方法都配置相同的事务属性信息。
     * 作用于方法：当类配置了@Transactional，方法也配置了@Transactional，方法的事务会覆盖类的事务配置信息。
     * 作用于接口：不推荐这种使用方法，因为一旦标注在Interface上并且配置了Spring AOP 使用CGLib动态代理，将会导致@Transactional注解失效。
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CrmAdminUser updateCrmAdminUser(long userId, String userName) {
        CrmAdminUser crmAdminUser = crmAdminUserMapper.selectByPrimaryKey(userId);
        crmAdminUser.setRealName(userName);
        crmAdminUserMapper.updateByPrimaryKey(crmAdminUser);
        return crmAdminUser;
    }
}
